/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysLoginLogService.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月22日
 */
package com.sirdc.modules.sys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.dao.SysLoginDao;
import com.sirdc.modules.sys.databean.SysLoginLogDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.util.HolidayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysLoginLogDao;
import com.sirdc.modules.sys.entity.SysLoginLog;
import com.sirdc.modules.sys.filter.SysLoginLogFilter;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.utils.StringUtils;
import com.sirdc.modules.utils.date.DateUtils;


/**
 * 
 *
 */
@Service
public class SysLoginLogService extends StringPKService<SysLoginLog> {
	
	@Autowired
	private SysLoginLogDao sysLoginLogDao;
	@Autowired
	private SysTableService tableService;
	@Autowired
    private SysLoginDao sysLoginDao;
	@Autowired
	private DeptService deptService;
	@Override
	protected BaseDao<SysLoginLog, String> getDao() {
		return sysLoginLogDao;
	}
	public void save(SysLoginLog obj, String userId){
		Date date = new Date();
		String date1 = HolidayUtils.sdf1.format(date);
		String time = HolidayUtils.sdf2.format(date);
		obj.setSysId(tableService.updateMaxSysId("t_sys_login_log",null));
		obj.setCreateUser(userId);
		obj.setUpdUser(userId);
		obj.setCreateDate(date1);
		obj.setCreateTime(time);
		obj.setUpdDate(date1);
		obj.setUpdTime(time);
		getDao().save(obj);
	}
	public List<SysLoginLogDataBean> query(SysLoginLogFilter filter) {
//        List<SysLoginLogDataBean> loginLogDataBeans=new ArrayList<>();
        List<SysLoginLogDataBean> sysLoginLogDataBeans = sysLoginLogDao.queryList(filter);
		for (SysLoginLogDataBean dataBean : sysLoginLogDataBeans) {
			String roleId = dataBean.getRoleId();
			 String roleName="01".equals(roleId)?"县领导":"02".equals(roleId)?"主要领导":"03".equals(roleId)?"分管领导":"04".equals(roleId)?"经办人":"未知";
			 dataBean.setRoleName(roleName);
		}
        //所有用户
       /* List<SysLogin> list=sysLoginDao.queryLog(filter );
		Paging paging = filter.getPaging();
		for (SysLogin login : list) {
            String username = login.getUsername();
			String userId = login.getSysId();
			String deptName = deptService.getById(login.getDeptId()).getDeptName();
			SysLoginLogDataBean dataBean = new SysLoginLogDataBean();
			dataBean.setUserId(login.getSysId());
            dataBean.setUserName(username);
			dataBean.setDeptId(login.getDeptId());
			dataBean.setRoleId(login.getRoleId());
			dataBean.setDeptName(deptName);
            //登录次数
            int i=0;
			int db=0;//待办个数

				//登录过
				filter.setUserName(username);
				i =sysLoginLogDao.queryCount(filter);

				*//*for (SysLoginLog sysLoginLog : sysLoginLogs) {
					if (userId.equals(sysLoginLog.getUserId())){
						i++;
					}
					String demo = sysLoginLog.getDemo();//当前待办个数
					if (StringUtils.isNotBlank(demo)){
						if (db<Integer.parseInt(demo)){
							db=Integer.parseInt(demo);
						}
					}
				}*//*
			dataBean.setDemo(db+"");
            dataBean.setLoginNum(i+"");
            loginLogDataBeans.add(dataBean);
        }*/

        return sysLoginLogDataBeans;
	}
	
	@Override
	public void beforeSave(SysLoginLog entity) {
		entity.setSysId(tableService.updateMaxSysId("t_sys_login_log", null));
	}
	
	/**
	 * 保存登陆日志记录
	 * @author: weihuang.peng
	 * @param principal
	 */
	//@Transactional(readOnly=true)
	public void saveLoginLog(SysPrincipal principal) {
		SysLoginLog loginLog = new SysLoginLog();
		loginLog.setDeptId(principal.getDeptId());
		loginLog.setLoginDate(DateUtils.getDate());
		loginLog.setLoginTime(DateUtils.getTime());
		save(loginLog);
	}
	
	/**
	 * 更新登陆时间(登陆信息只会插入一次)
	 * @author: weihuang.peng
	 * @param entity
	 */
	public void updateLoginLog(SysPrincipal principal) {
		SysLoginLog loginLog = getByUserIdLoginDate(principal.getUserId(), DateUtils.getDate());
		if (loginLog == null) {
			//TODO 填充信息
			loginLog = new SysLoginLog();
			loginLog.setDeptId(principal.getDeptId());
			loginLog.setLoginDate(DateUtils.getDate());
			loginLog.setLoginTime(DateUtils.getTime());
			save(loginLog);
		}
	}
	
	/**
	 * 更新登出
	 * @author: weihuang.peng
	 * @param entity
	 */
	public void updateLogoutLog(SysPrincipal principal) {
		SysLoginLog loginLog = getByUserIdLoginDate(principal.getUserId(), DateUtils.getDate());
		if (loginLog != null) {
			//TODO 填充信息
			loginLog.setLogoutDate(DateUtils.getDate());
			loginLog.setLogoutTime(DateUtils.getTime());
			update(loginLog);
		}
	}
	
	/**
	 * 根据UserId获取登陆日志
	 * @author: weihuang.peng
	 * @param userId
	 * @return
	 */
	public SysLoginLog getByUserIdLoginDate(String userId, String date) {
		if (StringUtils.isNotBlank(date)) {
			date = DateUtils.getDate();
		}
		return sysLoginLogDao.getByUserIdLoginDate(userId, date);
	}
}
